package com.ytsp.auxiliary.mail.impl;

import com.ytsp.auxiliary.mail.MailConfigResource;
import com.ytsp.auxiliary.rpc.IMailSendService;

import java.util.List;

/**
 * Created by HUJUN on 2016/9/26.
 */
public class MailSendServiceImpl implements IMailSendService {

    @Override
    public boolean send(String recipient, String subject, String content) {
        MailSendHelper themail = new MailSendHelper(
                MailConfigResource.getConfig(MailConfigResource.SMTP_HOST));
        themail.setNeedAuth(true);
        themail.setNeedSSL(true);
        themail.setNamePass(
                MailConfigResource.getConfig(MailConfigResource.MAIL_USER),
                MailConfigResource.getConfig(MailConfigResource.MAIL_PSW));
        // 设置模板
        try {
            if (themail.setSubject(subject) == false)
                return false;
            if (themail.setBody(content) == false)
                return false;
            if (themail.setTo(recipient) == false)
                return false;
            if (themail.setFrom(MailConfigResource
                    .getConfig(MailConfigResource.MAIL_FROM)) == false)
                return false;
            if (themail.sendout() == false)
                return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;

    }

    @Override
    public boolean send(List<String> recipients, String subject, String content) {
        MailSendHelper themail = new MailSendHelper(
                MailConfigResource.getConfig(MailConfigResource.SMTP_HOST));
        themail.setNeedAuth(true);
        themail.setNeedSSL(true);
        themail.setNamePass(
                MailConfigResource.getConfig(MailConfigResource.MAIL_USER),
                MailConfigResource.getConfig(MailConfigResource.MAIL_PSW));
        // 设置模板
        try {
            if (themail.setSubject(subject) == false)
                return false;
            if (themail.setBody(content) == false)
                return false;
            if (themail.setTo(recipients) == false)
                return false;
            if (themail.setFrom(MailConfigResource
                    .getConfig(MailConfigResource.MAIL_FROM)) == false)
                return false;
            if (themail.sendout() == false)
                return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean send(String recipient, String subject, String content, String fileAffix) {
        MailSendHelper themail = new MailSendHelper(
                MailConfigResource.getConfig(MailConfigResource.SMTP_HOST));
        themail.setNeedAuth(true);
        themail.setNeedSSL(true);
        themail.setNamePass(
                MailConfigResource.getConfig(MailConfigResource.MAIL_USER),
                MailConfigResource.getConfig(MailConfigResource.MAIL_PSW));
        try {
            if (themail.setSubject(subject) == false)
                return false;
            if (themail.setBody(content) == false)
                return false;
            if (themail.addFileAffix(fileAffix) == false)
                return false;
            if (themail.setTo(recipient) == false)
                return false;
            if (themail.setFrom(MailConfigResource
                    .getConfig(MailConfigResource.MAIL_FROM)) == false)
                return false;
            if (themail.sendout() == false)
                return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean send(List<String> recipients, String subject, String content, String fileAffix) {
        MailSendHelper themail = new MailSendHelper(
                MailConfigResource.getConfig(MailConfigResource.SMTP_HOST));
        themail.setNeedAuth(true);
        themail.setNeedSSL(true);
        themail.setNamePass(
                MailConfigResource.getConfig(MailConfigResource.MAIL_USER),
                MailConfigResource.getConfig(MailConfigResource.MAIL_PSW));
        try {
            if (themail.setSubject(subject) == false)
                return false;
            if (themail.setBody(content) == false)
                return false;
            if (themail.addFileAffix(fileAffix) == false)
                return false;
            if (themail.setTo(recipients) == false)
                return false;
            if (themail.setFrom(MailConfigResource
                    .getConfig(MailConfigResource.MAIL_FROM)) == false)
                return false;
            if (themail.sendout() == false)
                return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
